package com.example.demo.spring.service;

import com.example.demo.spring.spring.BeanPostProcessor;
import com.example.demo.spring.spring.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @className: LiukaiBeanPostProcessor
 * @copyright: Copyright (C) 2019-2028, Inc. All rights reserved.
 * @company: 中兴新云.财务云
 * @author: 刘凯
 * @date: 2024/7/3
 * @Description: com.example.demo.spring.spring
 */
@Component
public class LiukaiBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(String beanName, Object bean) {

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(String beanName, Object bean) {
        if(beanName.equals("userService")){
            Object o = Proxy.newProxyInstance(LiukaiBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("切面逻辑");
                    return method.invoke(bean, args);
                }
            });
            return o;
        }

        return bean;
    }
}
